<template>
  <ElDrawer v-model="opened" :title="formType" size="40%" destroy-on-close>
    <ElSkeleton v-if="loading" animated :rows="10" />
    <ElForm v-else :model="form" :rules="rules" ref="formRef" status-icon :disabled="isView">
      <ElFormItem label="姓名" :label-width="labelWidth" prop="name">
        <ElInput v-model="form.name" :disabled="true" />
      </ElFormItem>
      <ElFormItem label="头像" :label-width="labelWidth" prop="photo">
        <UploadImg v-model:imageUrl="form.photo!" :api="uploadAvatar" :file-size="5">
          <template #tip>头像最大为 5M</template>
        </UploadImg>
      </ElFormItem>
      <ElFormItem label="所属部门" :label-width="labelWidth" prop="orgName">
        <ElInput v-model="form.orgName" />
      </ElFormItem>
      <ElFormItem label="职务" :label-width="labelWidth" prop="position">
        <ElInput v-model="form.position" />
      </ElFormItem>
    </ElForm>
    <template #footer>
      <ElButton v-if="!isView" type="primary" @click="onSaveClicked">保存</ElButton>
      <ElButton @click="onCancelClicked">取消</ElButton>
    </template>
  </ElDrawer>
</template>

<script setup lang="ts">
import { reactive, ref, onMounted, unref, computed } from 'vue'
import { ElForm, ElFormItem, FormRules, FormInstance } from 'element-plus'
import { FormType } from '@/enums'
import { Person, getPersonDetail, saveOrUpdatePerson } from '@api/tby/mountains/person'
import { uiUpload } from '@api/upload'
import UploadImg from '@/components/Upload/Img.vue'

const labelWidth = 100
const opened = ref(false)
const loading = ref(true)
const formType = ref<FormType>()
const formRef = ref<FormInstance>()
const form = ref<Partial<Person.Form>>({})

onMounted(async () => {
  console.log('进来了')
})

function uploadAvatar(formData: FormData) {
  return uiUpload('avatar', formData)
}

// 是否查看
const isView = computed(() => {
  return formType.value === FormType.VIEW
})

// 是否新增
const isAdd = computed(() => {
  return formType.value === FormType.ADD
})

const rules = reactive<FormRules>({
  navId: [{ required: true, message: '请选择无人机', trigger: 'blur' }],
  photo: [{ required: true, message: '请上传头像', trigger: 'blur' }],
  orgName: [
    { required: true, message: '请输入所属部门', trigger: 'blur' },
    { max: 20, message: '所属部门最大20个字符', trigger: 'blur' }
  ],
  position: [
    { required: true, message: '请输入职务', trigger: 'blur' },
    { max: 20, message: '职务最大20个字符', trigger: 'blur' }
  ]
})

const open = async (type: FormType, id?: number) => {
  form.value = {}
  formType.value = type
  opened.value = true
  if (isAdd.value) {
    loading.value = false
  } else {
    try {
      const { data } = await getPersonDetail(id!)
      loading.value = false
      form.value = data
      console.log('form', form.value)
    } finally {
      loading.value = false
    }
  }
  formRef.value?.clearValidate()
}

function close() {
  loading.value = true
  opened.value = false
  form.value = {}
}

const onSaveClicked = () => {
  if (!formRef.value) {
    return
  }
  formRef.value?.validate(valid => {
    if (!valid) {
      return
    }
    const formData = { ...unref(form) }
    saveOrUpdatePerson(formData).then(() => {
      sendSuccess()
      close()
    })
  })
}

const onCancelClicked = () => {
  close()
}

defineExpose({
  open,
  close
})

const emit = defineEmits(['success'])

function sendSuccess() {
  emit('success')
}
</script>

<style scoped lang="scss">
.mo-input--number {
  border: 1px solid #dcdfe6;
  display: flex;
  border-radius: 4px;
  .el-input-number--mini {
    flex: 1;
  }
  ::v-deep .el-input__inner {
    border: none !important;
  }
}
.define-append {
  width: 40px;
  display: inline-block;
  background: #f5f7fa;
  padding: 0px 3px;
  border-left: none;
  height: 32px;
  line-height: 32px;
  color: #909399;
  font-size: 12px;
  text-align: center;
}
</style>
